using System;
using System.Collections;
using System.Data;
using BeeldBank.Business;
using BeeldBank.Business.Collection;

namespace BeeldBank.Business.Base
{
    /// <summary>
    /// Provides database independant methods for Setting.
    /// </summary>
    public class SettingBase : BusinessBase
    {
        internal SettingBase(System.Data.DataRow row)
            : base(row)
        {
            this.OnPrepareForSave += new PrepareForSaveHandler(this.PrepareForSave);
        }

        public delegate void PrepareForSaveHandler();
        public event PrepareForSaveHandler OnPrepareForSave;

        #region property bool EnforceSave
        /// This property is used to indicate if the
        /// object in the save event should save, even
        /// if the object contents (marked as stereotype NOTENFORCESAVE) is not saved
        private bool _EnforceSave = false;
        public bool EnforceSave
        {
            get
            {
                return _EnforceSave;
            }
            set
            {
                _EnforceSave = value;
            }

        }
        #endregion

        #region PrepareForSaveBase()
        public void PrepareForSaveBase()
        {
            if (!this.PrepareForSaveFinished)
            {
                if (OnPrepareForSave != null)
                {
                    OnPrepareForSave();
                }
                this.CanLoad = false;
                // call prepareforsave on all dependant objects.

                this.CanLoad = true;
                this.PrepareForSaveFinished = true;
            }
        }
        #endregion

        #region PrepareForSave()
        public virtual void PrepareForSave()
        {
        }
        #endregion

        #region PreSave()
        public virtual void PreSave()
        {
        }
        #endregion

        #region Save()
        public void Save()
        {
            bool IsSaveNeeded = ContentChanged();

            if (IsDirty)
            {
                this.ModifiedBy = _luw.UserName;
                if (this.State == BusinessState.New)
                {
                    this.CreatedBy = _luw.UserName;
                    BeeldBank.Data.Setting.Insert(this._dataRow, _luw.Transaction);

                }

                if ((this.State == BusinessState.Changed) && (IsSaveNeeded))
                {
                    BeeldBank.Data.Setting.Update(this._dataRow, _luw.Transaction);
                }

                if (this.State == BusinessState.Deleted)
                {
                    BeeldBank.Data.Setting.Delete(this._dataRow, _luw.Transaction);

                }
                IsDirty = false;
            }
        }
        #endregion

        #region OldSetting
        private Setting _oldSetting;
        public Setting OldSetting
        {
            get
            {
                if (_oldSetting == null && this.State != BusinessState.New)
                {
                    // Create the new object in a completely separate luw,
                    // otherwise a reference the the current object is returned.
                    Luw luw = new Luw(this.Luw.UserName);
                    _oldSetting = luw.NewSetting(BeeldBank.Data.Setting.Open(this.Id).Rows[0]);
                }
                return _oldSetting;
            }
        }
        #endregion

        #region Id
        /// <summary>
        /// 
        /// </summary>
        public int Id
        {
            get
            {
                return Convert.ToInt32(_dataRow["Id"]);
            }
            set
            {
                _dataRow["Id"] = value;
                this.IsDirty = true;
            }
        }
        public bool IdIsNull
        {
            get
            {
                return _dataRow["Id"] == Convert.DBNull;
            }
        }
        public void IdSetNull()
        {
            _dataRow["Id"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Name
        /// <summary>
        /// Name of the Name-Value pair.
        /// </summary>
        public string Name
        {
            get
            {
                return Convert.ToString(_dataRow["Name"]);
            }
            set
            {
                _dataRow["Name"] = value;
                this.IsDirty = true;
            }
        }
        public bool NameIsNull
        {
            get
            {
                return _dataRow["Name"] == Convert.DBNull;
            }
        }
        public void NameSetNull()
        {
            _dataRow["Name"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Value
        /// <summary>
        /// Value of the Nam-Value pair
        /// </summary>
        public string Value
        {
            get
            {
                return (_dataRow["Value"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Value"]);
            }
            set
            {
                _dataRow["Value"] = value;
                this.IsDirty = true;
            }
        }
        public bool ValueIsNull
        {
            get
            {
                return _dataRow["Value"] == Convert.DBNull;
            }
        }
        public void ValueSetNull()
        {
            _dataRow["Value"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region Description
        /// <summary>
        /// Omschrijving van deze systeem instelling
        /// </summary>
        public string Description
        {
            get
            {
                return (_dataRow["Description"] == Convert.DBNull) ? "" : Convert.ToString(_dataRow["Description"]);
            }
            set
            {
                _dataRow["Description"] = value;
                this.IsDirty = true;
            }
        }
        public bool DescriptionIsNull
        {
            get
            {
                return _dataRow["Description"] == Convert.DBNull;
            }
        }
        public void DescriptionSetNull()
        {
            _dataRow["Description"] = Convert.DBNull;
            this.IsDirty = true;
        }
        #endregion

        #region CreatedBy
        public String CreatedBy
        {
            get
            {
                return Convert.ToString(_dataRow["CreatedBy"]);
            }
            set
            {
                _dataRow["CreatedBy"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region CreatedAt
        public DateTime CreatedAt
        {
            get
            {
                return Convert.ToDateTime(_dataRow["CreatedAt"]);
            }
            set
            {
                _dataRow["CreatedAt"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region ModifiedBy
        public String ModifiedBy
        {
            get
            {
                return Convert.ToString(_dataRow["ModifiedBy"]);
            }
            set
            {
                _dataRow["ModifiedBy"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region ModifiedAt
        public DateTime ModifiedAt
        {
            get
            {
                return Convert.ToDateTime(_dataRow["ModifiedAt"]);
            }
            set
            {
                _dataRow["ModifiedAt"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region Version
        public int Version
        {
            get
            {
                return _dataRow["Version"] == Convert.DBNull ? -1 : Convert.ToInt32(_dataRow["Version"]);
            }
            set
            {
                _dataRow["Version"] = value;
                this.IsDirty = true;
            }
        }
        #endregion

        #region ContentChanged
        /// <summary>
        /// ContentChanged
        /// This function checks whether the content of a record is changed, dependant on the "EnforceSave" 
        /// variable. If EnforceSave is false, all the fields which have not been marked "NOTENFORCINGSAVE" as
        /// a stereotype in the design are checked. The contents of the fields are checked against the old values
        /// if they are not equal,the result is true. If the contents are equal, the result is false.
        /// 
        /// If EnforceSave is true, the result is always true.
        /// </summary>
        /// <returns>boolean indicating whether the Content is Changed</returns>
        public bool ContentChanged()
        {
            // Change enforced by Class Variables
            if (this.EnforceSave) { return (true); }
            if (this.IdIsNull) { return (true); }

            // Change enforced by the attributes

            #region Id
            /// Id is excluded because it is the primary key
            #endregion

            #region Name

            if (this.NameIsNull != this.OldSetting.NameIsNull) { return (true); }
            if (this.Name != this.OldSetting.Name) { return (true); }

            #endregion

            #region Value

            if (this.ValueIsNull != this.OldSetting.ValueIsNull) { return (true); }
            if (this.Value != this.OldSetting.Value) { return (true); }

            #endregion

            #region Description

            if (this.DescriptionIsNull != this.OldSetting.DescriptionIsNull) { return (true); }
            if (this.Description != this.OldSetting.Description) { return (true); }

            #endregion


            return false;
        }
        #endregion

        #region WhereAmI
        /// <summary>
        /// Provides the WhereAmI display text for a Setting with the specified id
        /// </summary>
        /// <param name="id">Id of the Setting.</param>
        /// <return>>WhereAmI display text</returns>
        public static string WhereAmI(int id)
        {
            string whereAmI = "";
            if (id == 0)
            {
                whereAmI = "New Setting";
            }
            else
            {
                DataTable table = BeeldBank.Data.Setting.Show(id);

                whereAmI = whereAmI + table.Rows[0]["Name"].ToString();

            }
            return whereAmI;
        }
        #endregion

    }
}